import json

from fastapi import FastAPI, Request

app = FastAPI()

@app.get("/v1/ping")
async def qr(request: Request):
    ping = request.query_params.get("ping", 0)
    print(ping)
    return "PONG"

# 获取⼆维码
@app.post("/v1/iron/qr")
async def qr(request: Request):
    data = await request.json()
    print(json.dumps(data, indent=4, ensure_ascii=False))
    return {
        "success": True,
        "errCode": None,
        "errMessage": None,
        "data": {
            "clientId": "",
            "qrUrl": "https://device-api.ktangtang.com:3001/v1/iron/18EF3ACBB492/qr-img.png"
        }
    }


# 账密/IC卡认证-获取用户信息
@app.post("/v1/iron/auth")
async def basic_auth(request: Request):
    data = await request.json()
    print(json.dumps(data, indent=4, ensure_ascii=False))
    if data["signInType"] == "01" and data["userCode"] != "chj":
        return {
            "success": False,
            "errCode": None,
            "errMessage": "用户信息不存在",
            "data": None
        }
    else:
        nick_name = "basic" if data["signInType"] == "01" else "ic"
        return {
            "success": True,
            "errCode": None,
            "errMessage": None,
            "data": {
                "clientId": "",
                "user": {
                    "userCode": "oSGFJ48CqH50GN8ohn4i-PujCxzg",
                    "nickName": f"tan90-{nick_name}",
                    "name": "",
                    "avatarUrl": "https://nuocaimespublic.oss-cn-beijing.aliyuncs.com/athlete/2024-11-21/27eed9/1732166761619.jpg",
                    "sex": 1,
                    "birthday": "1991-02-15",
                    "heartRate": 90,
                    "height": 175,
                    "weight": 65,
                    "phone": "",
                    "email": ""
                }
            }
        }


# 人脸识别认证-获取用户信息
@app.post("/v1/iron/face")
async def face_auth(request: Request):
    data = await request.json()
    print(json.dumps(data, indent=4, ensure_ascii=False))
    return {
        "success": True,
        "errCode": None,
        "errMessage": None,
        "data": {
            "clientId": "",
            "user": {
                "userCode": "oSGFJ48CqH50GN8ohn4i-PujCxzg",
                "nickName": "tan90-face",
                "name": "",
                "avatarUrl": "https://nuocaimespublic.oss-cn-beijing.aliyuncs.com/athlete/2024-11-21/27eed9/1732166761619.jpg",
                "sex": 1,
                "birthday": "1991-02-15",
                "heartRate": 90,
                "height": 175,
                "weight": 65,
                "phone": "",
                "email": ""
            }
        }
    }


# 设备在线状态通知
@app.post("/v1/iron/online-status")
async def online_status(request: Request):
    data = await request.json()
    print(json.dumps(data, indent=4, ensure_ascii=False))
    return {
        "success": True,
        "errCode": None,
        "errMessage": None,
        "data": None
    }


# 设备运行状态通知
@app.post("/v1/iron/status")
async def status(request: Request):
    data = await request.json()
    print(json.dumps(data, indent=4, ensure_ascii=False))
    return {
        "success": True,
        "errCode": None,
        "errMessage": None,
        "data": {
            "clientId": ""
        }
    }


# 瞬时数据推送
@app.post("/v1/iron/instant")
async def instant(request: Request):
    data = await request.json()
    print(json.dumps(data, indent=4, ensure_ascii=False))
    return {
        "success": True,
        "errCode": None,
        "errMessage": None,
        "data": None
    }


# 汇总数据推送
@app.post("/v1/iron/summary")
async def summary(request: Request):
    data = await request.json()
    print(json.dumps(data, indent=4, ensure_ascii=False))
    return {
        "success": True,
        "errCode": None,
        "errMessage": None,
        "data": None
    }


if __name__ == '__main__':
    import uvicorn

    uvicorn.run(app, host="0.0.0.0", port=8888)
